<template>
  <!-- 图片墙 -->
  <el-upload v-model:file-list="fileList" list-type="picture-card" :on-preview="handlePictureCardPreview"
    :on-remove="handleRemove" accept="image/png,image/gif,image/jpg,image/jpeg" :before-upload="beforeUpload"
    action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15">
    <el-icon>
      <Plus />
    </el-icon>
  </el-upload>
  <span>上传图片功能维护中...</span>

  <el-dialog v-model="dialogVisible">
    <img w-full :src="dialogImageUrl" alt="图片实现异常" />
  </el-dialog>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { Plus } from '@element-plus/icons-vue'
import { ElMessage, UploadProps, UploadUserFile } from 'element-plus'
import axios from 'axios';
const fileList = ref<UploadUserFile[]>([
  {
    name: 'xiaozhi and pikachu',
    url: 'https://s2.loli.net/2022/05/24/tsTAdRamSBLfE6x.jpg',
  },
  {
    name: 'food.jpeg',
    url: 'https://s2.loli.net/2022/03/12/HMCAKm5tJ2w9i7W.jpg'
  },
  {
    name: 'food.jpeg',
    url: 'https://s2.loli.net/2022/03/12/9uwXUnOvAZIjblS.jpg',
  },
])

const dialogImageUrl = ref('')
const dialogVisible = ref(false)
//上传前校验
const beforeUpload = (file: File) => {
  const isLt2M = file.size / 1024 / 1024 < 10
  if (!isLt2M) {
    ElMessage.error('上传图片大小不能超过 10MB！')
    return;
  }
  let fd = new FormData()
  fd.append('file', file)
  // 阿里巴巴API图床
  // 上传失败，未来考虑
  // axios({
  //   url: "https://api.uomg.com/api/image.ali",
  //   method: "post",
  //   data: fd,
  //   headers: { 'Content-Type': 'multipart/form-data' },
  // }).then(res => {
  //   console.log(res);
  // })
  return false;
}

const handleRemove: UploadProps['onRemove'] = (uploadFile, uploadFiles) => {
  console.log(uploadFile, uploadFiles)
}

const handlePictureCardPreview: UploadProps['onPreview'] = (uploadFile) => {
  dialogImageUrl.value = uploadFile.url!
  dialogVisible.value = true
}
</script>